﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.VehicleReport.BLL;
using System.Data;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;

public partial class Police_SecurityEvaluate_MendRecodList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }
    void init()
    {
        BindDepartmentId();
        BindUnit();
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (DepartmentId.Substring(0, 4) != "1300" && DepartmentId.Substring(4, 1) == "0")
        {
            ddlDepartment.SelectedValue = DepartmentId.Substring(0, 4) + "000";
            BindUnit();
            ddlDepartment.Enabled = false;

        }
        else if (DepartmentId.Substring(0, 4) != "1300" && DepartmentId.Substring(4, 1) == "3")
        {
            ddlDepartment.SelectedValue = DepartmentId.Substring(0, 4) + "000";
            BindUnit();
            ddlUnit.SelectedValue = DepartmentId;
            ddlDepartment.Enabled = false;
            ddlUnit.Enabled = false;
        }

        ListPageContral1.SetFirstPage();
        BindData(0);
    }

    void BindDepartmentId()
    {
        ddlDepartment.Items.Add(new ListItem("---单位---", ""));
        string where = "DepartmentId like '____000' and departmentId<>'1300000' ";
        List<DepartmentInfo> departments = Department.GetList(where);
        for (int i = 0; i < departments.Count; i++)
        {
            ddlDepartment.Items.Add(new ListItem(departments[i].AliasName, departments[i].DepartmentID));
        }


    }

    void BindUnit()
    {
        ddlUnit.Items.Clear();
        ddlUnit.Items.Add(new ListItem("-------单位--------", ""));
        if (ddlDepartment.SelectedValue != "")
        {
            string where = "DepartmentId like '" + ddlDepartment.SelectedValue.Substring(0, 4) + "3__' ";
            List<DepartmentInfo> departments = Department.GetList(where);
            for (int i = 0; i < departments.Count; i++)
            {
                ddlUnit.Items.Add(new ListItem(departments[i].ShortName, departments[i].DepartmentID));
            }
        }


    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindUnit();
    }
    protected void lkbQuery_Click(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindData(0);
    }

    void BindData(int pageIndex)
    {

        //验证
        try
        {
            DateTime dt = new DateTime();
            if (txtStartDate.Text != "")
            {
                dt = Convert.ToDateTime(txtStartDate.Text);
            }
            if (txtEndDate.Text != "")
            {
                dt = Convert.ToDateTime(txtEndDate.Text);
            }
            if (txtFinishED.Text != "")
            {
                dt = Convert.ToDateTime(txtFinishED);
            }
            if (txtFinishSD.Text != "")
            {
                dt = Convert.ToDateTime(txtFinishSD);
            }
            lblPrompt.Text = "";
        }
        catch
        {
            lblPrompt.Text = "提示：时间录入不正确！";
            return;
        }
        string DepartmentId = ddlUnit.SelectedValue;
        string where = "";
        //单位
        if (ddlUnit.SelectedValue != "")
        {
            where += " DepartmentId='" + ddlUnit.SelectedValue + "' ";
        }
        else if (ddlDepartment.SelectedValue != "")
        {
            where += " DepartmentId like '" + ddlDepartment.SelectedValue.Substring(0, 4) + "%' ";
        }

        //时间
        if (txtStartDate.Text != "")
        {
            where += where == "" ? "" : " and ";

            where += " mendTime>='" + txtStartDate.Text + "' ";
        }

        if (txtEndDate.Text != "")
        {
            where += where == "" ? "" : " and ";

            where += " mendTime<'" + Convert.ToDateTime(txtEndDate.Text).AddDays(1).ToString() + "' ";
        }

        //时间


        if (txtFinishSD.Text != "")
        {
            where += where == "" ? "" : " and ";

            where += " finishTime>='" + txtFinishSD.Text + "' ";
        }

        if (txtFinishED.Text != "")
        {
            where += where == "" ? "" : " and ";

            where += " finishTime<='" + Convert.ToDateTime(txtFinishED.Text).AddDays(1).ToString() + "' ";
        }

        //类型 
        string Flags = "";
        for (int i = 0; i < cblFlag.Items.Count; i++)
        {
            if (cblFlag.Items[i].Selected)
            {
                Flags += cblFlag.Items[i].Value + ",";
            }
        }
        if (Flags.Length > 0)
        {
            Flags = Flags.Remove(Flags.Length - 1);
            where += where == "" ? "" : " and ";
            where += " flag in (" + Flags + ") ";
        }


        ListPageContral1.ItemCount = MendRecord.GetMendRecordsCount(where);
        DataSet ds = MendRecord.GetAllMendRecords(ListPageContral1.PageSize, pageIndex, where);
        gvMeeting.DataSource = ds.Tables[0];
        gvMeeting.DataBind();
    }


    protected void gvMeeting_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            DataRowView drv = e.Row.DataItem as DataRowView;

            Label lblVehicleName = e.Row.FindControl("lblVehicleName") as Label;

            Guid Id = new Guid(drv["Vid"].ToString());
            VehicleInfo info = Vehicle.GetDetail(Id);
            if (info != null)
            {
                lblVehicleName.Text = info.VehicleName;
            }
            else
            {
                lblVehicleName.Text = drv["VehicleName"].ToString() == "" ? "此车辆已删除" : drv["VehicleName"].ToString();
                lblVehicleName.ForeColor = System.Drawing.Color.BlueViolet;
            }

            Label lblDeviceName = e.Row.FindControl("lblDeviceName") as Label;
            if (drv["DeviceId"] != DBNull.Value)
            {
                Guid DeviceId = new Guid(drv["DeviceId"].ToString());
                DeviceInfo device = Device.GetDevice(DeviceId);

                if (device != null)
                {
                    lblDeviceName.Text = device.DeviceName;
                }
                else
                {
                    lblDeviceName.Text = "此器材已删除";
                    lblDeviceName.ForeColor = System.Drawing.Color.BlueViolet;
                }

            }
            else
            {
                lblDeviceName.Text = "";

            }
            Label lblType = e.Row.FindControl("lblType") as Label;
            if (Convert.ToInt32(drv["flag"]) == 0)
            {
                lblType.Text = "车辆损坏";
            }
            else
            {
                lblType.Text = "器材损坏";
            }
            Label lblDepartment = e.Row.FindControl("lblDepartment") as Label;
            DepartmentInfo department = Department.GetDepartment(drv["DepartmentId"].ToString());
            if (department != null)
            {
                string DepartmentName = department.FullName;
                lblDepartment.Text = DepartmentName;
            }
            else
            {
                lblDepartment.Text = "此部门已删除";
            }
            HyperLink hplView = e.Row.FindControl("hplView") as HyperLink;
            hplView.NavigateUrl = "MendRecordView.aspx?Flag=" + drv["flag"] + "&id=" + drv["id"];

            Label lblFinishTime = e.Row.FindControl("lblFinishTime") as Label;
            lblFinishTime.Text = Convert.ToDateTime(drv["FinishTime"].ToString()).Year == 9999 ? "" : Convert.ToDateTime(drv["FinishTime"].ToString()).ToString("yyyy-MM-dd");
        }
    }

    protected void gvMeeting_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindData(PageIndex);
    }
}
